#### II Replication ####
### purpose: replicating table D1 ####
# installing packages ----

list.of.packages = c('estimatr', 'stargazer', 'MASS')
new.packages <- list.of.packages[!(list.of.packages %in% 
                                     installed.packages()[,"Package"])]

if(length(new.packages)) install.packages(new.packages)

# loading packages ----

suppressPackageStartupMessages(
  {
    library(estimatr)
    library(stargazer)
    library(MASS)
  }
)

# loading in data ----
irt <- readRDS("~data/rep_data4_irt.RDS")

dt <- readRDS("~data/rep_data1.RDS")

dt_irt <- dt %>% 
  left_join(irt)

# Table D1 ----
form <- "Mean ~ modi0_num_firm + highskill + highedu + medianincome + unemploy + black + white + hispanic + foreignborn +  EvanProt + Catholic + MainProt + nominate1 + app + foreign  + labor + corp + bank + prezSupport + as.factor(state)"

res111 <-  lm(form, data = dt_irt %>% filter(cong==111))

res112 <-  lm(form, data = dt_irt %>% filter(cong==112))

res113 <-  lm(form, data = dt_irt %>% filter(cong==113))

res114 <-  lm(form, data = dt_irt %>% filter(cong==114))

res115 <-  lm(form, data = dt_irt %>% filter(cong==115))

stargazer(res111, res112, res113, res114, res115,
          model.names =TRUE, 
          column.labels=c("111th", "112th", "113th", "114th", "115th"), 
          header=FALSE, 
          keep=c("modi0_num_firm", 
                 "highskill", "highedu",
                 "medianincome", "unemploy",
                 "black", "white", "hispanic",
                 "foreignborn",
                 "EvanProt","Catholic", "MainProt",
                 "nominate1", "app", "foreign", 
                 "labor", "corp", "bank",
                 "prezSupport"),
          covariate.labels=c("DevFirm Num",
                             "HighSkill", 
                             "HighEdu",
                             "ln(MedianIncome)", 
                             "Unemploy", 
                             "Black", 
                             "White", 
                             "Hispanic", 
                             "ForeignBorn",
                             "EvanProt", "Catholic",  "MainProt", 
                             "Nominate1",
                             "ApproprtnCommt",
                             "ForeignCommt", 
                             "LaborPAC", 
                             "CorpPAC", 
                             "BankPAC",
                             "PrezSupport"), 
          align=T, 
          font.size="scriptsize", 
          omit.stat=c("ser", "bic", "aic", "ll", "theta"), 
          column.sep.width="0pt", 
          dep.var.labels="Dependent Variable: FA Position (Index created by IRT Model)", 
          title = "Results", omit.table.layout="l",
          type="latex", 
          add.lines = list(c("state FE", "Y", "Y", "Y", "Y", "Y")))
